﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Zimmerreservierung.Controller;
using System.Data.SQLite;
using Zimmerreservierung.Model;
using System.Collections.Generic;

namespace Tests
{
    [TestClass]
    public class UnitTestZimmer
    {
        public TestContext TestContext { get; set; }
        private static ZimmerController controller;

        [ClassInitialize()]
        public static void ClassInitialize(TestContext context)
        {
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=testdb.dat"))
            {
                connection.Open();
                using (SQLiteCommand command = new SQLiteCommand(connection))
                {
                    command.CommandText = "DROP TABLE IF EXISTS Zimmer";
                    command.ExecuteNonQuery();

                    command.CommandText = "CREATE TABLE IF NOT EXISTS Zimmer ( ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR(100) NOT NULL, MaxBelegzahl INTEGER NOT NULL, "
                                                                        + " PreisEZ REAL NOT NULL, PreisDZ REAL, PreisDreifachZ REAL, PreisEZmitKind REAL, PreisEZmit2Kind REAL, PreisDZmitKind REAL );";
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }

            controller = ZimmerController.getInstance();
            controller.startConnection("Data Source=testdb.dat");
        }

        [TestMethod]
        public void testAddZimmer()
        {
            try
            {
                bool ok = controller.StoreZimmer("Zimmer 1", 3, 10.0, 20.0, 30.0, 20.0, 30.0, 40.0);
                Assert.AreEqual(true, ok);

                ok = controller.StoreZimmer("Zimmer 2", 3, 20.0, 30.0, 40.0, 30.0, 40.0, 50.0);
                Assert.AreEqual(true, ok);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testGetZimmers()
        {
            try
            {
                List<Zimmer> zimmer = controller.GetZimmeren();
                Assert.AreEqual(2, zimmer.Count);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testGetZimmer()
        {
            try
            {
                Zimmer zimmer = controller.GetZimmer(1);
                Assert.IsNotNull(zimmer);

                Assert.AreEqual(1, zimmer.Id);
                Assert.AreEqual("Zimmer 1", zimmer.Name);
                Assert.AreEqual(3, zimmer.Belegzahl);
                Assert.AreEqual(10.0, zimmer.Erwachsener);
                Assert.AreEqual(20.0, zimmer.ZweiErwachsene);
                Assert.AreEqual(30.0, zimmer.DreiErwachsene);
                Assert.AreEqual(20.0, zimmer.ErwachsenerKind);
                Assert.AreEqual(30.0, zimmer.ErwachsenerZweiKinder);
                Assert.AreEqual(40.0, zimmer.ZweiErwachseneKind);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testEditZimmer()
        {
            try
            {
                bool ok = controller.UpdateZimmer(1, "Zimmer Update", 4, 20.0, 30.0, 40.0, 30.0, 40.0, 50.0);
                Assert.AreEqual(true, ok);

                Zimmer zimmer = controller.GetZimmer(1);
                Assert.IsNotNull(zimmer);

                Assert.AreEqual(1, zimmer.Id);
                Assert.AreEqual("Zimmer Update", zimmer.Name);
                Assert.AreEqual(4, zimmer.Belegzahl);
                Assert.AreEqual(20.0, zimmer.Erwachsener);
                Assert.AreEqual(30.0, zimmer.ZweiErwachsene);
                Assert.AreEqual(40.0, zimmer.DreiErwachsene);
                Assert.AreEqual(30.0, zimmer.ErwachsenerKind);
                Assert.AreEqual(40.0, zimmer.ErwachsenerZweiKinder);
                Assert.AreEqual(50.0, zimmer.ZweiErwachseneKind);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testDeleteZimmer()
        {
            try
            {
                bool ok = controller.RemoveZimmer(1);
                Assert.AreEqual(true, ok);

                Zimmer zimmer = controller.GetZimmer(1);
                Assert.IsNull(zimmer);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
    }
}
